package org.idol.generator.codegen.mybatis.javamapper.method;

import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.AbstractJavaMapperMethodGenerator;

import java.util.Set;
import java.util.TreeSet;

/**
 * @author coco
 * @date 2020-06-27 03:21
 **/
public class InsertListMethodGenerator extends AbstractJavaMapperMethodGenerator {

    private boolean isSimple;

    public InsertListMethodGenerator(boolean isSimple) {
        super();
        this.isSimple = isSimple;
    }


    @Override
    public void addInterfaceElements(Interface interfaze) {

        Set<FullyQualifiedJavaType> importedTypes = new TreeSet<>();

        Method method = new Method(introspectedTable.getInsertListStatementId());

        method.setReturnType(FullyQualifiedJavaType.getIntInstance());
        method.setVisibility(JavaVisibility.PUBLIC);
        method.setAbstract(true);

        FullyQualifiedJavaType parameterType = FullyQualifiedJavaType.getNewListInstance();
        if (isSimple) {
            FullyQualifiedJavaType fullyQualifiedJavaType = new FullyQualifiedJavaType(
                    introspectedTable.getBaseRecordType());
            parameterType.addTypeArgument(fullyQualifiedJavaType);
            importedTypes.add(fullyQualifiedJavaType);
        } else {
            FullyQualifiedJavaType fullyQualifiedJavaType = introspectedTable.getRules()
                    .calculateAllFieldsClass();
            parameterType.addTypeArgument(fullyQualifiedJavaType);
            importedTypes.add(fullyQualifiedJavaType);
        }


        importedTypes.add(parameterType);
        method.addParameter(new Parameter(parameterType, "records")); //$NON-NLS-1$

        context.getCommentGenerator().addGeneralMethodComment(method,
                introspectedTable);

        addMapperAnnotations(method);

        if (context.getPlugins().clientInsertMethodGenerated(method, interfaze,
                introspectedTable)) {
            addExtraImports(interfaze);
            interfaze.addImportedTypes(importedTypes);
            interfaze.addMethod(method);
        }
    }

    public void addMapperAnnotations(Method method) {
        // extension point for subclasses
    }

    public void addExtraImports(Interface interfaze) {
        // extension point for subclasses
    }
}
